<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Events
 *
 * @author tien.nguyen
 */
class Webservices_Events {
    
    private $_modelUsers = null;
    private $_modelEvents = null;
    
    public function __construct() {
        $this->_modelEvents = new Application_Model_Events();
        $this->_modelUsers = new Application_Model_Users();
    }
    
    /**
     * Get category by id
     * @param string $username
     * @param string $password
     * @param string $categoryId
     * @return string
     */
    public function getCategoryById($username, $password, $categoryId){
        $res = array();
        $modelDbEventCategory = new Application_Model_DbTable_EventCategories();
        
        $eventCategory = $modelDbEventCategory->find($categoryId)->current();
        if($eventCategory){
            $res = $eventCategory->toArray();
            $modelDbEventCategory->convertFieldObjectItem($res);
        }
        
        return json_encode($res);
    }
    
    /**
     * Get list events
     * @param string $username
     * @param string $password 
     * @return string
     */
    public function getEvents($username, $password){
        $xml = '';
        if($this->_modelUsers->login($username, $password)){
            $events = $this->_modelEvents->getEvents($username);
            Webservices_XML_Format::convertOjbToXml($events, $xml);
        }
        return $this->viewxml($xml);
    }
    
    /**
     * Get event categories 
     * @param string $username
     * @param string $password
     * @return string 
     */
    public function listEventCategories($username, $password){
        $eventCategories = array();
        
        //instance model event categories
        $modelEventCategories = new Application_Model_DbTable_EventCategories();
        if($this->_modelUsers->login($username, $password)){
            $eventCategories = $this->_modelEvents->listEventCategory();
            if(count($eventCategories) > 0){
                $eventCategories = $eventCategories->toArray();
                $modelDbEventCategory = new Application_Model_DbTable_EventCategories();
                $modelDbEventCategory->convertFiledObject($eventCategories);
            }
        }
        
        return json_encode($eventCategories);
    }
    
    /**
     * List events
     * @author Tien Nguyen
     * @param string $username
     * @param string $password
     * @param string $lat
     * @param string $lng
     * @return string 
     */
    public function listEvents($username, $password, $lat, $lng){
        $events = array();
        if($this->_modelUsers->login($username, $password)){
            $events = $this->_modelEvents->listEvents($username, $lat, $lng);
        }
        return json_encode($events);
    }
    
    /**
     * Get events by user id
     * @param string $username
     * @param string $password
     * @param string $userId
     * @param string $start
     * @param string $limit
     * @return string
     */
    public function listEventsByUserId($username, $password, $userId, 
        $start, $limit){
       
        $events = array();
         if($this->_modelUsers->login($username, $password)){
            $events = $this->_modelEvents->listEventsByUserId($userId, $start, $limit);
            
        }
        return json_encode($events);
    }
    
    /**
     * Add events
     * @param string $username
     * @param string $password
     * @param string $data
     * @return int
     */
    public function addEvent($username, $password, $data){
        $res = 0;
        if($this->_modelUsers->login($username, $password)){
            $object = json_decode($data);
            $arr = array();
            Webservices_Array_Format::convertObjectToArray($object, $arr);
            $res = $this->_modelEvents->addEvent($arr);
        }
        return $res;
    }
    
    /**
     * remove event
     * @param string $username
     * @param string $password
     * @param int $eventId
     * @return boolean
     */
    public function removeEvent($username, $password, $eventId){
        $res =  false;
        
        if($this->_modelUsers->login($username, $password)){
            $res = $this->_modelEvents->removeEvent($eventId);    
        }
        
        return $res;
    }
    
    /**
     * Edit events
     * @param string $username
     * @param string $password
     * @param string $data
     * @return boolean
     */
    public function editEvent($username, $password, $data){
        $result = false;
        
        if($this->_modelUsers->login($username, $password)){
            $data = json_decode($data);
            $rs = array();
            Webservices_Array_Format::convertObjectToArray($data, $rs);
            
            if($this->_modelEvents->editEvent($rs) > 0){
                $result = true;
            }
            
        }
        
        return $result;
    }
    
    /**
     * Get event
     * @param string $username
     * @param string $password
     * @param int $eventId
     * @return string
     **/
    public function getEvent($username, $password, $eventId){
         $data = array();
         
         if($this->_modelUsers->login($username, $password)){
            $res = $this->_modelEvents->getEvent($eventId);
            if($res){
                $data = $res->toArray();
            }
         }
         
         $data = $this->_modelEvents->convertObject($data);
         return json_encode($data);
    }
    
    /**
     * View xml
     * @param string $xml
     * @return string
     */
    public function viewxml($xml) {
        return '<?xml version="1.0"?><data>' . $xml . '</data>';
    }
}

?>
